function [best_schedule, best_score] = optimizer_random_sampling_bottleneck(initial_schedule, initial_history, initial_score, G, code_map, is_ancestor, options)
    % 阶段一优化器：增加"瓶颈攻击"逻辑
    %
    % V2 修正:
    % - [Bug修复] 修复了因错误使用垂直拼接符(;)导致的vertcat维度不一致问题。
    %
    % V1 最终修正:
    % - [核心逻辑修复] 增加了对外部验证函数 is_schedule_dependency_valid 的调用。
    %   彻底修复了原有的"移动节点"逻辑中存在的严重依赖约束漏洞。
    %   确保此函数产出的所有解都100%满足拓扑排序约束。
    %
    current_schedule = initial_schedule;
    % ---【错误修正】--- 使用传入的 initial_score 初始化 best_score
    best_score = initial_score;
    current_history = initial_history;
    num_nodes = numnodes(G);
    iterations_without_improvement = 0;
    
    for iter = 1:options.iterations
        [~, peak_idx] = max(current_history);
        search_space_indices = 1:peak_idx;
        if isempty(search_space_indices) || peak_idx == 0, search_space_indices = 1:num_nodes; end
        
        candidate_schedules = cell(options.batch_size, 1);
        candidate_scores = ones(options.batch_size, 1) * inf;
        
        parfor k = 1:options.batch_size
            local_schedule = current_schedule; 
            
            % 随机选择一个节点进行移动
            node_idx_to_move = search_space_indices(randi(length(search_space_indices)));
            node_to_move = local_schedule(node_idx_to_move);
            
            % --- 原有逻辑：计算一个理论上的"有效移动范围" ---
            % (注意：此逻辑有缺陷，但可作为快速生成候选方案的方式，最终正确性由后续的完整验证保证)
            ancestors = find(is_ancestor(:, node_to_move)); 
            descendants = find(is_ancestor(node_to_move, :));
            
            positions_in_schedule = 1:num_nodes;
            ancestor_positions = positions_in_schedule(ismember(local_schedule, ancestors));
            latest_ancestor_pos = 0; 
            if ~isempty(ancestor_positions), latest_ancestor_pos = max(ancestor_positions); end
            
            descendant_positions = positions_in_schedule(ismember(local_schedule, descendants));
            earliest_descendant_pos = num_nodes + 1; 
            if ~isempty(descendant_positions), earliest_descendant_pos = min(descendant_positions); end
            
            valid_range = (latest_ancestor_pos + 1):(earliest_descendant_pos - 1);
            valid_range(valid_range == node_idx_to_move) = [];
            
            if isempty(valid_range), continue; end
            
            % 随机选择一个新位置并生成临时方案
            new_pos = valid_range(randi(length(valid_range)));
            
            temp_schedule = local_schedule;
            temp_schedule(node_idx_to_move) = []; % 从原位置移除
            
            % 插入到新位置 (注意：由于移除了一个元素，位置需要调整)
            if new_pos > node_idx_to_move
                new_pos = new_pos - 1; 
            end
            
            % --- 【Bug修复】使用逗号进行水平拼接 ---
            temp_schedule = [temp_schedule(1:new_pos-1), node_to_move, temp_schedule(new_pos:end)];
            
            % --- 【核心逻辑修复】---
            % 对生成的临时方案进行完整的、权威的约束验证
            if is_schedule_dependency_valid(temp_schedule, is_ancestor) && is_l0_schedule_valid(temp_schedule, G, code_map)
                candidate_schedules{k} = temp_schedule;
                [candidate_scores(k), ~] = calculate_max_vstay(temp_schedule, G, code_map);
            end
        end
        
        [best_batch_score, best_batch_idx] = min(candidate_scores);
        
        % 检查是否有非空的有效候选方案产生
        if ~isempty(best_batch_score) && best_batch_score < best_score
            % 确保 best_batch_idx 指向的候选方案是存在的
            if ~isempty(candidate_schedules{best_batch_idx})
                current_schedule = candidate_schedules{best_batch_idx};
                best_score = best_batch_score;
                [~, current_history] = calculate_max_vstay(current_schedule, G, code_map);
                iterations_without_improvement = 0;
                fprintf('  阶段一 > 迭代 %d / %d: 发现更优解！新分数为: %d\n', iter, options.iterations, best_score);
            else
                % 这种情况很少见，但可能发生：最低分inf对应的cell为空
                iterations_without_improvement = iterations_without_improvement + 1;
                fprintf('  阶段一 > 迭代 %d / %d: 未发现有效更优解 (当前最优: %d, 连续未改进次数: %d)\n', iter, options.iterations, best_score, iterations_without_improvement);
            end
        else
            iterations_without_improvement = iterations_without_improvement + 1;
            fprintf('  阶段一 > 迭代 %d / %d: 未发现更优解 (当前最优: %d, 连续未改进次数: %d)\n', iter, options.iterations, best_score, iterations_without_improvement);
        end
        
        if iterations_without_improvement >= options.patience
            fprintf('  阶段一 > 连续 %d 次迭代未发现改进，提前终止。\n', options.patience);
            break;
        end
    end
    
    best_schedule = current_schedule;
end